//
//  NYSubjectTitleCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/3.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYSubjectTitleCell.h"
#import "UIImage+UrlSize.h"
@interface NYSubjectTitleCell ()<DTAttributedTextContentViewDelegate, DTLazyImageViewDelegate>
@property(nonatomic, strong) DTAttributedTextContentView *titleAttrView;
@property(nonatomic, strong) NYHtmlHelper *htmlHelper;
@property(nonatomic, strong) UIView *bottomContentView;
@end
@implementation NYSubjectTitleCell
//+ (CGFloat)heightForObject:(NICellObject *)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
//{
//        NYSubjectModel *subjectModel = (NYSubjectModel *)object.userInfo;
//        return [tableView fd_heightForCellWithIdentifier:identifier cacheByIndexPath:indexPath configuration:^(NYSubjectTitleCell * cell) {
//
//                cell.indexPath = indexPath;
//                cell.subjectModel = subjectModel;
//        }];
//}
+ (BOOL)shouldAppendIndexPathToReuseIdentifier
{
        return YES;
}
- (BOOL)shouldUpdateCellWithObject:(NICellObject *)object atIndexPath:(NSIndexPath *)indexPath
{
        self.indexPath = indexPath;
        self.subjectModel = (NYSubjectModel *)object.userInfo;
        return YES;
}

- (void)setSubjectModel:(NYSubjectModel *)subjectModel
{
        _subjectModel = subjectModel;
        [self updateContent];
}
- (void)updateContent
{
        NSString *resString = self.subjectModel.question_title.copy;
        
        NSRange range = [resString rangeOfString:@"<body>"];
        NSString *replaceString = [NSString stringWithFormat:@"<body>%d.",(int)self.indexPath.section + 1];
        if (range.location == NSNotFound) {
                range = [resString rangeOfString:@"<p>"];
                replaceString = [NSString stringWithFormat:@"<p>%d.",(int)self.indexPath.section + 1];
        }
        if (range.location != 0) {
                resString = [NSString stringWithFormat:@"%d.%@",(int)self.indexPath.section + 1,resString];
        } else {
                resString = [resString stringByReplacingCharactersInRange:range withString:replaceString];
        }
        
        resString = [self.htmlHelper getImgSizeAndReplaceHtmlString:resString];
        
        self.titleAttrView.attributedString = [self.htmlHelper attributedStringForHtmlString:resString maxImageSize:CGSizeMake(__WGWidth(self.bottomContentView.width), CGFLOAT_MAX)];
        CGFloat suggestHeight = [self.titleAttrView  suggestedFrameSizeToFitEntireStringConstraintedToWidth:__MAIN_SCREEN_WIDTH__ - __WGWidth(32)].height;
        [self.titleAttrView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.bottom.equalTo(self.bottomContentView);
                make.height.mas_equalTo(suggestHeight);
        }];
}

- (void)setupView
{
        [super setupView];
        UIView *contentView = [[UIView alloc] init];
        [self.contentView addSubview:contentView];
        self.bottomContentView = contentView;
        [contentView addSubview:self.titleAttrView];
        
        [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).mas_offset(__WGHeight(15));
                make.left.equalTo(self.contentView).mas_offset(__WGHeight(16));
                make.bottom.equalTo(self.contentView).mas_offset(-__WGHeight(5)).priorityLow();
                make.right.equalTo(self.contentView).mas_offset(-__WGHeight(16));
        }];
        [self.titleAttrView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.bottom.equalTo(contentView);
        }];
}

/// MARK: - Lazy
- (DTAttributedTextContentView *)titleAttrView
{
        if (_titleAttrView == nil) {
                _titleAttrView = [self.htmlHelper createAttributedContentView];
        }
        return _titleAttrView;
}

- (NYHtmlHelper *)htmlHelper
{
        if (_htmlHelper == nil) {
                _htmlHelper = [[NYHtmlHelper alloc] init];
        }
        return _htmlHelper;
}
@end
